import logging
from functools import wraps
def log(func):
    logger = logging.getLogger(func)
    def decorator(func):
        @wraps(func)
        def tmp(request, *args, **kwargs):
            name = func.__name__
            meta = str(request.META)
            user_agent = request.META['HTTP_USER_AGENT'].lower()
            if 'iPhone' in user_agent:
                client = 'IOS'
            elif  'android' in user_agent:
                client = 'Android'
            elif 'HTTP_ORIGIN' in meta:
                client = 'Web'
            else:
                client = 'DB'
            ip = request.META['REMOTE_ADDR']
            method = request.method
            mes = '[username:%s] [client:%s] [functions:%s] [ip:%s] [%s]' % (request.user, client, name, ip, method)
            logger.info(mes)
            return func(request, *args, **kwargs)
        return tmp
    return decorator

def clog(func):
    logger = logging.getLogger(func)
    def decorator(func):
        @wraps(func)
        def tmp(self, request, *args, **kwargs):
            # print(getattr(self, 'get'))
            name = str(self)
            meta = str(request.META)
            user_agent = request.META['HTTP_USER_AGENT'].lower()
            if 'iPhone' in user_agent:
                client = 'IOS'
            elif  'android' in user_agent:
                client = 'Android'
            elif 'HTTP_ORIGIN' in meta:
                client = 'Web'
            else:
                client = 'DB'
            ip = request.META['REMOTE_ADDR']
            method = request.method
            mes = '[username:%s] [client:%s] [functions:%s] [ip:%s] [%s]' % (request.user, client, name, ip, method)
            logger.info(mes)
            return func(self, request, *args, **kwargs)
        return tmp
    return decorator


